package com.example.administrator.newjiajiesong.test;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.example.administrator.newjiajiesong.R;


/**
 * 日期：2017.01.21
 * <p>
 * 作者：xudiwei
 * <p>
 * 描述：SwipeRefreshLayout 与RecyclerView的组合控件
 */
public class RLListPager extends FrameLayout {

    private static final String TAG = "RLListPager";

    private SwipeRefreshLayout mRefreshLayout;
    private RecyclerView mRecyclerView;
    private OnRLRefreshAndLoadMoreListener mListener;
    private int loadMoreLimit = 5;

    public RLListPager(Context context) {
        this(context, null);
    }

    public RLListPager(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RLListPager(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        //刷新布局
        mRefreshLayout = new SwipeRefreshLayout(getContext());
        mRefreshLayout.setOnRefreshListener(new MyOnRefreshListener());
        mRefreshLayout.setProgressViewOffset(true, 0,
                (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 80, getResources().getDisplayMetrics()));
        mRefreshLayout.setColorSchemeResources(R.color.colorGreen);

        //列表布局
        mRecyclerView = new RecyclerView(getContext());
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRefreshLayout.addView(mRecyclerView, new SwipeRefreshLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));

        this.addView(mRefreshLayout, new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT));

        mRecyclerView.addOnScrollListener(new MyOnScrollListener());

    }

    public RecyclerView getRecyclerView() {
        return mRecyclerView;
    }

    /**
     * 设置适配器
     *
     * @param adapter
     */
    public void setAdapter(RecyclerView.Adapter adapter) {
        mRecyclerView.setAdapter(adapter);
    }

    /**
     * 设置刷新动画
     *
     * @param refreshing
     */
    public void setRefresh(boolean refreshing) {
        mRefreshLayout.setRefreshing(refreshing);
    }

    public boolean isRefresh() {
        return mRefreshLayout.isRefreshing();
    }

    public int getLoamoreLimit() {
        return loadMoreLimit;
    }

    /**
     * 设置触发上拉加载更多的最少item默认为5个item才能触发
     *
     * @param loadMoreLimit
     */
    public void setloadmoreLimit(int loadMoreLimit) {
        this.loadMoreLimit = loadMoreLimit;
    }

    /**
     * 刷新
     */
    private class MyOnRefreshListener implements SwipeRefreshLayout.OnRefreshListener {
        @Override
        public void onRefresh() {
            //刷新布局
            if (null != mListener) {
                mListener.onRefresh();
            }
        }
    }

    /**
     * 加载更多
     */
    private class MyOnScrollListener extends RecyclerView.OnScrollListener {
        private LinearLayoutManager mLayoutManager;

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);

            if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                if (null == mLayoutManager) {
                    mLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                }
                RecyclerView.Adapter adapter = recyclerView.getAdapter();
                if (null != adapter) {
                    int itemCount = adapter.getItemCount();
                    if (itemCount > loadMoreLimit && mLayoutManager.findLastVisibleItemPosition() == itemCount - 1) {

                        //加载更多
                        if (null != mListener) {
                            mListener.onLoadMore();
                        }
                    }
                }

            }
        }

    }

    public void scrollToPositionWithOffset(int position,int offset){
        mRecyclerView.scrollToPosition(position);
        mRecyclerView.scrollBy(0,offset);

    }

    public void setOnRLRefreshAndLoadMoreListener(OnRLRefreshAndLoadMoreListener listener) {
        this.mListener = listener;
    }

    /**
     * 刷新加载更多回调接口
     */
    public interface OnRLRefreshAndLoadMoreListener {

        /**
         * 下拉刷新时回调
         */
        void onRefresh();

        /**
         * 上拉加载更多时回调
         */
        void onLoadMore();
    }
}
